package org.citra.citra_emu.utils;

import android.text.TextUtils;
import android.webkit.MimeTypeMap;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 *
 * @author jie.li
 */
public class StringUtil {

    //region boolean isEmpty(String str)判断字符串是否为null或空字符串

    /**
     * 判断字符串是否为null或空字符串
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() <= 0;

    }
    //endregion

    //region String getMimeType(String url) 获取MIME类型
    public static String getMimeType(String url) {
        String type = null;
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        if (extension != null) {
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            type = mime.getMimeTypeFromExtension(extension);
        }
        return type;
    }
    //endregion

    public static String getExtension(String url) {
        if (!TextUtils.isEmpty(url)) {
            int fragment = url.lastIndexOf('#');
            if (fragment > 0) {
                url = url.substring(0, fragment);
            }

            int query = url.lastIndexOf('?');
            if (query > 0) {
                url = url.substring(0, query);
            }

            int filenamePos = url.lastIndexOf('/');
            String filename = 0 <= filenamePos ? url.substring(filenamePos + 1) : url;
            if (!filename.isEmpty()) {
                int dotPos = filename.lastIndexOf('.');
                if (0 <= dotPos) {
                    return filename.substring(dotPos + 1);
                }
            }
        }
        return "";
    }

    //region boolean isTrimEmpty(String str)判断字符串前后截掉空字符后,是否为null或空字符串

    /**
     * 判断字符串前后截掉空字符后,是否为null或空字符串
     *
     * @param str
     * @return
     */
    public static boolean isTrimEmpty(String str) {
        if (str == null) {
            return true;
        }

        str = str.trim();
        return isEmpty(str);
    }
    //endregion

    //region boolean isContainsChinese判断字符串中是否包含中文

    /**
     * 判断字符串中是否包含中文
     *
     * @param str
     * @return
     */
    public static boolean isContainsChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        return m.find();
    }
    //endregion

    //region boolean isInt(String str)判断是否为数字
    public static boolean isInt(String str) {
        Pattern p = Pattern.compile("^[-+]?[0-9]");
        Matcher m = p.matcher(str);
        return m.matches();
    }
    //endregion

    //region String JieQuStr(String str, String str_a, String str_b)截取指定位置的字符串

    /**
     * 截取字符串
     *
     * @param str
     * @param str_a 起始字符串
     * @param str_b 结束字符串
     * @return
     */
    public static String JieQuStr(String str, String str_a, String str_b) {
        int JieQuStr_i = 0;
        int JieQuStr_j = 0;
        try {
            if (str_a != null) {
                JieQuStr_i = str.indexOf(str_a);
                if (JieQuStr_i == -1) return null;
                JieQuStr_i += str_a.length();
            }
            if (str_b == null) {
                JieQuStr_j = str.length();
            } else {
                JieQuStr_j = str.indexOf(str_b, JieQuStr_i);
                if (JieQuStr_j == -1) return null;
            }
            return str.substring(JieQuStr_i, JieQuStr_j);
        } catch (Exception e) {
            return null;
        }
    }
    //endregion

    /**
     * 获取strings字符串中所有str字符所在的下标
     *
     * @param strings 母字符串
     * @param str     子字符串
     * @return 字符串在母字符串中下标集合，如果母字符串中不包含子字符串，集合长度为零
     */
    public static List<Integer> getIndex(String strings, String str) {
        List<Integer> list = new ArrayList<>();
        int flag = 0;
        while (strings.contains(str)) {
            //截取包含自身在内的前边部分
            String aa = strings.substring(0, strings.indexOf(str) + str.length());
            flag = flag + aa.length();
            list.add(flag - str.length());
            strings = strings.substring(strings.indexOf(str) + str.length());
        }
        return list;
    }

    //region 数字转换
    public @interface NUM_UNIT {
    }

    public static final int UNIT_TEN_THOUSAND = 10000;
    public static final int UNIT_THOUSAND = 1000;

    public static String getFriendlyNum(int num) {
        return getFriendlyNum(num, UNIT_TEN_THOUSAND, false);
    }

    public static String getFriendlyNum(int num, @NUM_UNIT int unit, boolean isChinese) {
        if (num >= unit) {
            DecimalFormat df = new DecimalFormat("#.#");
            String formatNum = df.format(num * 1f / unit);
            switch (unit) {
                case UNIT_TEN_THOUSAND:
                    if (isChinese) {
                        return formatNum + "万";
                    } else {
                        return formatNum + "w";
                    }
                case UNIT_THOUSAND:
                    if (isChinese) {
                        return formatNum + "千";
                    } else {
                        return formatNum + "k";
                    }
                default:
                    break;
            }
        }
        return num + "";
    }
    //endregion 数字转换

    /**
     * 从链接取到文件格式
     *
     * @return
     */
    public static String getFileFormatFromLink(String link) {
        if ((link != null) && (link.length() > 0)) {
            int dot = link.lastIndexOf('.');
            if ((dot > -1) && (dot < (link.length() - 1))) {
                return link.substring(dot);
            }
        }
        return "";
    }
}
